﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.NonlinearEquationSolvers
{
    /// <summary>
    /// This class represents an abstract layer for root finders, which need no derivation.
    /// </summary>
    [Serializable]
    public abstract class AbstractDerivativeNoNeedRootFinder : AbstractRootFinder
    {
        /// <summary>
        /// The function for finding the roots.
        /// </summary>
        private IRealFunction function;

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractDerivativeNoNeedRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        protected AbstractDerivativeNoNeedRootFinder(Polynomial polynomial)
        {
            if (polynomial == (Polynomial) null)
            {
                throw new ArgumentNullException("polynomial");
            }

            this.function = polynomial.Copy();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractDerivativeNoNeedRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        protected AbstractDerivativeNoNeedRootFinder(SimplePolynomial polynomial)
        {
            if (polynomial == (SimplePolynomial) null)
            {
                throw new ArgumentNullException("polynomial");
            }

            this.function = polynomial.Copy().ToPolynomial();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractDerivativeNoNeedRootFinder"/> class.
        /// </summary>
        /// <param name="function">The function for finding the roots.</param>
        protected AbstractDerivativeNoNeedRootFinder(IRealFunction function)
        {
            if (function == (IRealFunction) null)
            {
                throw new ArgumentNullException("function");
            }

            this.function = function;
        }

        /// <summary>
        /// Gets or sets the polynomial for finding the roots.
        /// </summary>
        /// <value>The polynomial for finding the roots.</value>
        public IRealFunction Function
        {
            get { return function; }
            set { function = value; }
        }
    }
}